<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input v-model="listQuery.commanyname" style="width: 200px;" class="filter-item" placeholder="请输入公司名称" @keyup.enter.native="handleFilter"/>
      <el-input v-model="listQuery.contactphone" style="width: 200px;" class="filter-item" placeholder="请输入公司联系电话" @keyup.enter.native="handleFilter"/>
      <el-input v-model="listQuery.owner" style="width: 200px;" class="filter-item" placeholder="请输入法人名称" @keyup.enter.native="handleFilter"/>
      <el-button v-waves v-if="btn_query" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <!--<el-button v-if="btn_add" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>-->
    </div>

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column width="150" align="center" label="公司名称">
        <template slot-scope="scope">
          <span>{{ scope.row.commanyname }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="公司地址">
        <template slot-scope="scope">
          <span>{{ scope.row.commanyaddress }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120" align="center" label="公司电话">
        <template slot-scope="scope">
          <span>{{ scope.row.contactphone }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120" align="center" label="公司工商注册码">
        <template slot-scope="scope">
          <span>{{ scope.row.commanyno }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="网站">
        <template slot-scope="scope">
          <span>{{ scope.row.web }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="法人名称">
        <template slot-scope="scope">
          <span>{{ scope.row.owner }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120" align="center" label="法人电话">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="邮箱">
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="监管单位">
        <template slot-scope="scope">
          <span v-for="option in supervisorOptions" :key="option">
            <p v-if="option.value===scope.row.supervisor">{{ option.label }}</p>
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="btn_edit" type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <!-- <el-button v-if="btn_delete" size="mini" type="danger" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="监管单位" prop="supervisor">
          <el-select v-model="temp.supervisor" class="filter-item select-width-100" placeholder="请选择">
            <el-option v-for="item in supervisorOptions" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="commanyname">
          <el-input v-model="temp.commanyname"/>
        </el-form-item>
        <el-form-item label="公司地址" prop="commanyaddress">
          <el-input v-model="temp.commanyaddress"/>
        </el-form-item>
        <el-form-item label="公司电话" prop="contactphone">
          <el-input v-model="temp.contactphone"/>
        </el-form-item>
        <el-form-item label="工商注册码" prop="commanyno">
          <el-input v-model="temp.commanyno"/>
        </el-form-item>
        <el-form-item label="法人名称" prop="owner">
          <el-input v-model="temp.owner"/>
        </el-form-item>
        <el-form-item label="法人电话" prop="phone">
          <el-input v-model="temp.phone"/>
        </el-form-item>
        <el-form-item label="公司网站" prop="web">
          <el-input v-model="temp.web"/>
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input v-model="temp.email"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMaintainorList, addMaintainor, updateMaintainor, deleteMaintainor } from '@/api/fire/maintainor/base'
import { selectLabelValue } from '@/api/fire/supervisor/base'
import { isNotNull, length, selectvalidator } from '@/utils/validate'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'Maintainor',
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        commanyname: '',
        contactphone: '',
        owner: ''
      },
      showReviewer: false,
      temp: {
        commanyname: '',
        commanyaddress: '',
        contactphone: '',
        commanyno: '',
        web: '',
        owner: '',
        phone: '',
        supervisor: '',
        email: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        commanyname: isNotNull('请输入公司名称'),
        commanyaddress: isNotNull('请输入公司地址'),
        contactphone: isNotNull('请输入公司电话').concat(selectvalidator('validateTelphone')),
        commanyno: isNotNull('公司工商注册码'),
        owner: isNotNull('请输入法定代表人'),
        web: selectvalidator('isUrl'),
        email: selectvalidator('isEMail'),
        phone: selectvalidator('validatePhone')
      },
      supervisorOptions: [],
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false
    }
  },
  created() {
    this.getList()
    this.btn_query = this.checkPermission(['maintainor:base:list'])
    this.btn_add = this.checkPermission(['maintainor:base:add'])
    this.btn_edit = this.checkPermission(['maintainor:base:update'])
    this.btn_delete = this.checkPermission(['maintainor:base:delete'])
    this.querySupervisorOptions()
  },
  methods: {
    getList() {
      this.listLoading = true
      getMaintainorList(this.listQuery).then(response => {
        this.list = response.data.context.records
        this.total = response.data.context.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        commanyname: '',
        commanyaddress: '',
        contactphone: '',
        commanyno: '',
        web: '',
        owner: '',
        phone: '',
        supervisor: '',
        email: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addMaintainor(this.temp).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateMaintainor(tempData).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMaintainor(row.proprietor).then(response => {
          if (response.data.status === '0') {
            this.getList()
            this.$notify({
              title: '成功',
              message: response.data.msg,
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify.error({
              title: '失败',
              message: response.data.msg,
              duration: 2000
            })
          }
        })
      })
    },
    querySupervisorOptions() {
      selectLabelValue().then(response => {
        if (response.data.status === '0') {
          this.supervisorOptions = response.data.context
        }
      })
    }
  }
}
</script>
